package no.ntnu.fp.services.impl;

import java.io.IOException;

import no.ntnu.fp.client.exceptions.NetworkException;
import no.ntnu.fp.messages.impl.AddMeetingMessage;
import no.ntnu.fp.messages.impl.DeleteMeetingMessage;
import no.ntnu.fp.messages.impl.MeetingRejectedMessage;
import no.ntnu.fp.messages.impl.RequestMeetingsForEmployeeMessage;
import no.ntnu.fp.messages.impl.UpdateMeetingMessage;
import no.ntnu.fp.model.Employee;
import no.ntnu.fp.model.Meeting;
import no.ntnu.fp.net.co.Connection;
import no.ntnu.fp.services.MeetingService;

public class MeetingServiceImpl extends EntityServiceImpl implements MeetingService {

	public MeetingServiceImpl(Connection connection) {
		super(connection);
	}

	@Override
	public void addMeeting(Meeting meeting) {
		AddMeetingMessage message = new AddMeetingMessage(meeting);
		try {
			connection.send(message.toString());
		} catch (IOException ex) {
			throw new NetworkException("Could not send AddMeetingMessage", ex);
		}
	}

	@Override
	public void updateMeeting(Meeting meeting) {
		UpdateMeetingMessage message = new UpdateMeetingMessage(meeting);
		try {
			connection.send(message.toString());
		} catch (IOException e) {
			throw new NetworkException("Could not send UpdateMeetingMessage", e);
		}
	}

	@Override
	public void deleteMeeting(Meeting meeting, Employee employee) {
		DeleteMeetingMessage message = new DeleteMeetingMessage(meeting, employee);
		try {
			connection.send(message.toString());
		} catch (IOException ex) {
			throw new NetworkException("Could not send DeleteMeetingMessage", ex);
		}
	}

	@Override
	public void fetchMeetingsForEmployee(Employee employee) {
		RequestMeetingsForEmployeeMessage message 
							= new RequestMeetingsForEmployeeMessage(employee);
		try {
			connection.send(message.toString());
		} catch (IOException ex) {
			throw new NetworkException(
					"Could not send RequestMeetingsForEmployeeMessage", ex);
		}
	}

	@Override
	public void rejectMeeting(Meeting meeting, Employee employee) {
		MeetingRejectedMessage message = new MeetingRejectedMessage(meeting, 
				employee);
		try {
			connection.send(message.toString());
		} catch (IOException e) {
			throw new NetworkException("Could not send MeetingRejectedMessage", e);
		}
		
	}
}
